<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->

    <el-card>
      <!-- 搜索与添加 -->
      <el-row>
        <el-col :span="7">
          <el-input placeholder="请输入名字" v-model="queryInfo.query" clearable @clear="getUserList">
<el-select v-model="select" slot="prepend" placeholder="请选择">
      <el-option label="已缴费" value="1"></el-option>
      <el-option label="未缴费" value="2"></el-option>
      <el-option label="确定" value="3"></el-option>
    </el-select>

            <el-button slot="append" icon="el-icon-search" @click="getUserList">


            </el-button>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-button @click="addDiglogVisible=true">添加</el-button>
          <el-button>导入</el-button>
          <el-button>导出</el-button>
         
        </el-col>
        <!-- <el-col :span="12">
           <el-tag
  v-for="tag in tags"
  :key="tag.name"
  closable
  
  :type="tag.type">
  {{tag.name}}
</el-tag>
        </el-col> -->
      </el-row>

      <!-- 用户展示 -->
      <el-table
    :data="userList"
    
    border
    :default-sort = "{prop: 'index', order: 'descending'}"
    >
     
        <el-table-column type="index" width="50" sortable></el-table-column>
        <el-table-column prop="userName"  width="100" label="姓名" sortable></el-table-column>
          <el-table-column prop="userSnNumber"  width="100" label="会员号" sortable></el-table-column>
        <el-table-column prop="userGender" width="50" label="性别" sortable>
          <template slot-scope="scope" >
            <p v-if="scope.row.userGender===1">男</p>
            <p v-if="scope.row.userGender===0">女</p>
            <p v-if="scope.row.userGender===null"></p>
          </template>
        </el-table-column>
        <el-table-column prop="userAge" width="50" label="年龄" sortable></el-table-column>
          <el-table-column prop="userDeptName" label="会员单位" sortable></el-table-column>
           <el-table-column prop="userCity" label="所在地" sortable></el-table-column>
        <!-- 状态区域 -->
        <el-table-column prop="userState" label="状态" width="100" sortable>
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.userState"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="userStateChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <!-- 操作区域 -->
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <!-- 修改 -->
              <el-tooltip
             
              class="item"
              effect="dark"
              content="修改用户"
              placement="top"
              :enterable="false"
            >
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditDialog(scope.row.userId)"></el-button>
              </el-tooltip>
            <!-- 删除 -->
             <el-popconfirm
  title="您确定删除吗？"
     	confirmButtonText='好的'
     	cancelButtonText="取消"
  @onConfirm="deletedUser(scope.row.userId)"
>
            <el-button type="danger" size="mini" icon="el-icon-delete"  slot="reference"></el-button>
             </el-popconfirm>
            <!-- 重置密码 -->
              <el-tooltip
             
              class="item"
              effect="dark"
              content="重置密码"
              placement="top"
              :enterable="false"
            >
            <el-button
            size="mini"
              type="warning"
              icon="el-icon-refresh-left"
              @click="restPassWord(scope.row.userId)"
            ></el-button>
            </el-tooltip>
            <!-- 发消息 -->
              <!-- <el-tooltip
             
              class="item"
              effect="dark"
              content="发送消息"
              placement="top"
              :enterable="false"
            >
            <el-button type="info" icon="el-icon-message" size="mini" @click="messageDialogVisible=true"></el-button>
            </el-tooltip> -->
            <!-- 角色按钮 -->
            <!-- <el-tooltip
             
              class="item"
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button type="warning" icon="el-icon-cpu" size="mini"></el-button>
            </el-tooltip> -->
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[10, 50, 100, 200]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 添加用户 -->
      <el-dialog title="添加用户" :visible.sync="addDiglogVisible" width="50%" @close="addUserClosed">
        <!-- 内容主体区 -->
        <el-form
          :model="addUserForm"
          :rules="addUserFormRules"
          ref="addUserFormRef"
          label-width="100px"
        >
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="addUserForm.userName"></el-input>
          </el-form-item>
          <el-form-item label="身份证号" prop="userCertnumber">
            <el-input v-model="addUserForm.userCertnumber"></el-input>
          </el-form-item>
          <el-form-item label="组织机构">
            <el-select v-model="addUserForm.userDeptId" placeholder="请选择组织机构">
              <el-option
                v-for="item in orgList"
                :key="item.id"
                :label="item.orgName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <!-- 增加用户底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDiglogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改用户 -->
      <el-dialog
  title="修改用户"
  :visible.sync="editUserDialogVisible"
  width="50%"
  >
  <el-form ref="editUserFormRef" :model="editUserForm"  :rules="editUserFormRules" label-width="80px">
  <el-form-item label="用户名">
    <el-input v-model="editUserForm.userName"></el-input>
  </el-form-item>
  <el-form-item label="身份证号">
    <el-input v-model="editUserForm.userCertnumber"></el-input>
  </el-form-item>
  <el-form-item label="组织机构">
    <el-input v-model="editUserForm.userDeptName"></el-input>
  </el-form-item>
  </el-form>
 <!-- 修改用户底部区域 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="editUserDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editUserDialogVisible = false">确 定</el-button>
  </span>
</el-dialog>

      <!-- 删除用户 -->

      <!-- 给用户发私信 -->
      <el-dialog
  title="写消息"
  :visible.sync="messageDialogVisible"
  width="30%"
  >
  <el-form ref="form"  label-width="80px">
  
    <el-input
  type="textarea"
  :rows="4"
  placeholder="请输入内容"
  >
</el-input>
  
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="messageDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="messageDialogVisible = false">确 定</el-button>
  </span>
</el-dialog>
      <!-- 分配角色 -->
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
       input1: '',
      input2: '',
      input3: '',
      select: '',
      //后区用户列表的参数
      queryInfo: {
        query: "",
        //当前的页数
        pageNum: 1,
        pageSize: 10
      },
      userList: [],
      total: 0,
      //控制添加用户对话框的显示与隐藏
      addDiglogVisible: false,
      //控制删除用户对话框的显示与隐藏
      deleteUserDialogVisible: false,
      //控制修改用户对话框的显示与隐藏
      editUserDialogVisible:false,
      //控制消息对话框的显示与隐藏
      messageDialogVisible:false,
      //添加用户表单
      addUserForm: {
        userName: "",
        userCertnumber: "",
        userDeptId: ""
      },
       tags: [
          { name: '会员', type: '' },
          { name: '已交费', type: 'success' },
           { name: '未缴费', type: 'danger' },
            
             { name: '成年人', type: 'success' },
          { name: '未成年人', type: 'info' },
          
          { name: '管理者', type: 'danger' }
        ],
      //添加表单的验证规则
      addUserFormRules: {
        userName: [
          {
            required: true,
            message: "请输入姓名",
            //失去焦点时触发
            trigger: "blur"
          },
          {
            min: 1,
            max: 10,
            message: "用户名的长度在1-10个字符之间",
            trigger: "blur"
          }
        ]
      },
      //会员单位
      orgList: [],
      //修改用户表单
      editUserForm: {
        userName: "xxx",
        userCertnumber: "",
        userDeptId: ""
      },
editUserFormRules: {
        userName: [
          {
            required: true,
            message: "请输入姓名",
            //失去焦点时触发
            trigger: "blur"
          },
          {
            min: 1,
            max: 10,
            message: "用户名的长度在3-10个字符之间",
            trigger: "blur"
          }
        ]
      },





    };
  },
  created() {
    this.getUserList();
    this.getOrgList();
  },
  methods: {
    
    async getUserList() {
      const { data: res } = await this.$http.get("user/getUsers", {
        params: this.queryInfo
      });
      if (res.code !== 200) {
        return this.$message.error("获取用户列表失败！");
      }

      this.userList = res.data.records;
      this.total = res.data.total;
    },
    //监听页码容量的改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getUserList();
    },
    //获取组织机构
    async getOrgList() {
      const { data: res } = await this.$http.get("/organization/getOrgList");
      if (res.code !== 200) return this.$message.error(res.msg);
      console.log(res.data);
      this.orgList = res.data;
    },
    //监听页码值改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getUserList();
    },
    //状态改变
    async userStateChange(userInfo) {
      const { data: res } = await this.$http.put(
        `user/updateUserState?userId=${userInfo.userId}&state=${userInfo.userState}`
      );

      if (res.code !== 200) {
        this.$message.error("获取用户列表失败！");
      } else if (res.code === 200) {
        this.$message.success("状态修改成功！");
      }
    },
    //监听添加用户对话框关闭事件
    addUserClosed() {
      this.$refs.addUserFormRef.resetFields();
    },
    //点击按钮添加新用户
    addUser() {
      this.$refs.addUserFormRef.validate(async valid => {
        if (!valid) return;
        //可以发起添加用户的网络请求
        const { data: res } = await this.$http.put(
          `user/saveUser?userName=${this.addUserForm.userName}&userCertnumber=${this.addUserForm.userCertnumber}&deptId=${this.addUserForm.userDeptId}`
        );
        if (res.code !== 200) {
          this.$message.error("获取用户列表失败！");
        } else if (res.code === 200) {
          this.$message.success("添加成功！");
          this.addDiglogVisible = false;
          this.getUserList();
        }
      });
    },
    //删除用户
    async deletedUser(userID) {
      const { data: res } = await this.$http.put(
        `user/deleteUser?userId=${userID}`
      );

      if (res.code !== 200) {
        this.$message.error("修改失败！");
      } else if (res.code === 200) {
        this.$message.success("删除成功！");
      }
      this.getUserList();
    },

    //重置密码
    async restPassWord(userID) {
      const { data: res } = await this.$http.put(
        `user/restPassWord?userId=${userID}`
      );

      if (res.code !== 200) {
        this.$message.error("修改失败！");
      } else if (res.code === 200) {
        this.$message.success("密码重置成功！");
      }
    },
    //展示用户编辑的对话框
   async showEditDialog(id){
this.editUserDialogVisible=true
const {data:res}=await this.$http.get(`user/getById?userId=${id}`)
 if (res.code !== 200) {
        this.$message.error("请求失败！");
      } 
      console.log(res.data)
this.editUserForm=res.data
this.editUserDialogVisible=true

    },
    //更新用户
  async  update(user){
const {data:res}=await this.$http.post("user/updateUser",{
params:user
 
});
console.log("!!!!"+res.data)

    }
  }
};
</script>
<style lang="less" scoped>
.el-card {
  justify-content: space-between;
}
.el-pagination {
  margin-top: 15px;
}
.el-dialog .el-input {
  width: 50%;
}
.el-main ,.el-table{
  line-height: 0;
}
</style>